home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 24
/
Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso
/
Aminet
/
comm
/
mail
/
Mutt089src.lha
/
Mutt-0.89i-AMIGA
/
src
/
lib.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-28
|
15KB
|
792 lines
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "mutt.h"
#include "mutt_regex.h"
#include "mutt_curses.h"
#include "mime.h"
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
#include <pwd.h>
#include <sys/stat.h>
#include <fcntl.h>
BODY *mutt_new_body (void)
{
BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
p->disposition = DISPATTACH;
p->use_disp = 1;
return (p);
}
BODY* mutt_dup_body (BODY *b)
{
BODY *bn;
bn = mutt_new_body();
memcpy(bn, b, sizeof (BODY));
return bn;
}
void mutt_free_body (BODY **p)
{
BODY *a = *p, *b;
while (a)
{
b = a;
a = a->next;
if (b->parameter)
mutt_free_parameter (&b->parameter);
if (b->unlink && b->filename)
unlink (b->filename);
safe_free ((void **) &b->filename);
safe_free ((void **) &b->content);
safe_free ((void **) &b->subtype);
safe_free ((void **) &b->description);
safe_free ((void **) &b->form_name);
if (b->hdr)
{
/* Don't free twice (b->hdr->content = b->parts) */
b->hdr->content = NULL;
mutt_free_header(&b->hdr);
}
if (b->parts)
mutt_free_body (&b->parts);
safe_free ((void **) &b);
}
*p = 0;
}
void mutt_free_parameter (PARAMETER **p)
{
PARAMETER *t = *p;
PARAMETER *o;
while (t)
{
safe_free ((void **) &t->attribute);
safe_free ((void **) &t->value);
o = t;
t = t->next;
safe_free ((void **) &o);
}
*p = 0;
}
void mutt_free_list (LIST **list)
{
LIST *p;
if (!list) return;
while (*list)
{
p = *list;
*list = (*list)->next;
safe_free ((void **) &p->data);
safe_free ((void **) &p);
}
}
HEADER *mutt_dup_header(HEADER *h)
{
HEADER *hnew;
hnew = mutt_new_header();
memcpy(hnew, h, sizeof (HEADER));
return hnew;
}
void mutt_free_header (HEADER **h)
{
mutt_free_envelope (&(*h)->env);
mutt_free_body (&(*h)->content);
safe_free ((void **) &(*h)->tree);
safe_free ((void **) &(*h)->path);
safe_free ((void **) h);
}
/* returns true if the header contained in "s" is in list "t" */
int mutt_matches_ignore (const char *s, LIST *t)
{
for (; t; t = t->next)
{
if (!strncasecmp (s, t->data, strlen (t->data)) || *t->data == '*')
return 1;
}
return 0;
}
/* prepend the path part of *path to *link */
void mutt_expand_link (char *newpath, const char *path, const char *link)
{
const char *lb = NULL;
size_t len;
/* link is full path */
if (*link == '/')
{
strfcpy (newpath, link, _POSIX_PATH_MAX);
return;
}
if ((lb = strrchr (path, '/')) == NULL)
{
/* no path in link */
strfcpy (newpath, link, _POSIX_PATH_MAX);
return;
}
len = lb - path + 1;
memcpy (newpath, path, len);
strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
}
char *mutt_expand_path (char *s, size_t slen)
{
char p[_POSIX_PATH_MAX] = "";
char *q = NULL;
if (*s == '~')
{
if (*(s + 1) == '/' || *(s + 1) == 0)
snprintf (p, sizeof (p), "%s%s", Homedir, s + 1);
else
{
struct passwd *pw;
q = strchr (s + 1, '/');
if (q)
*q = 0;
if ((pw = getpwnam (s + 1)))
snprintf (p, sizeof (p), "%s/%s", pw->pw_dir, q ? q + 1 : "");
else
{
/* user not found! */
if (q)
*q = '/';
return (NULL);
}
}
}
else if (*s == '=' || *s == '+')
snprintf (p, sizeof (p), "%s/%s", Maildir, s + 1);
else
{
if (*s == '>')
q = Inbox;
else if (*s == '<')
q = Outbox;
else if (*s == '!')
q = Spoolfile;
else if (*s == '-')
q = LastFolder;
else
return s;
if (!*q)
return s;
snprintf (p, sizeof (p), "%s%s", q, s + 1);
}
if (*p)
strfcpy (s, p, slen); /* replace the string with the expanded version. */
return (s);
}
/* returns TRUE if the given address belongs to the user. */
int mutt_addr_is_user (ADDRESS *addr)
{
char buf[LONG_STRING];
/* NULL address is assumed to be the user. */
if (!addr)
return 1;
if (!addr->mailbox)
return 0;
if (strcasecmp (addr->mailbox, Username) == 0)
{
if (!addr->host || *addr->host == '@' ||
strcasecmp (addr->host, Hostname) == 0 ||
strcasecmp (addr->host, Fqdn) == 0)
return 1;
}
if (Alternates.pattern)
{
buf[0] = 0;
mutt_simple_address (buf, sizeof (buf), addr);
if (regexec (Alternates.rx, buf, 0, NULL, 0) == 0)
return 1;
}
return 0;
}
void *safe_calloc (size_t nmemb, size_t size)
{
void *p;
if (!nmemb || !size)
return NULL;
if (!(p = calloc (nmemb, size)))
{
mutt_error ("Out of memory");
sleep (1);
mutt_exit (1);
}
return p;
}
void *safe_malloc (unsigned int siz)
{
void *p;
if (siz == 0)
return 0;
if ((p = (void *) malloc (siz)) == 0)
{
mutt_error ("Out of memory!");
sleep (1);
mutt_exit (1);
}
return (p);
}
void safe_realloc (void **p, size_t siz)
{
void *r;
if (siz == 0)
{
if (*p)
{
free (*p);
*p = NULL;
}
return;
}
if (*p)
r = (void *) realloc (*p, siz);
else
{
/* realloc(NULL, nbytes) doesn't seem to work under SunOS 4.1.x */
r = (void *) malloc (siz);
}
if (!r)
{
mutt_error ("Out of memory!");
sleep (1);
mutt_exit (1);
}
*p = r;
}
void safe_free (void **p)
{
if (*p)
{
free (*p);
*p = 0;
}
}
char *safe_strdup (const char *s)
{
char *p;
size_t l;
if (!s || !*s) return 0;
l = strlen (s) + 1;
p = (char *)safe_malloc (l);
memcpy (p, s, l);
return (p);
}
char *mutt_skip_whitespace (char *p)
{
SKIPWS (p);
return p;
}
int mutt_copy_bytes (FILE *in, FILE *out, size_t size)
{
char buf[2048];
size_t chunk;
while (size > 0)
{
chunk = (size > sizeof (buf)) ? sizeof (buf) : size;
if ((chunk = fread (buf, 1, chunk, in)) < 1)
break;
if (fwrite (buf, 1, chunk, out) != chunk)
{
dprint (1, (debugfile, "mutt_copy_bytes(): fwrite() returned short byte count\n"));
return (-1);
}
size -= chunk;
}
return 0;
}
void mutt_free_address (ADDRESS **p)
{
ADDRESS *t;
while (*p)
{
t = *p;
safe_free ((void **) &t->personal);
safe_free ((void **) &t->mailbox);
safe_free ((void **) &t->adl);
safe_free ((void **) &t->host);
*p = (*p)->next;
safe_free ((void **) &t);
}
}
char *mutt_get_parameter (const char *s, PARAMETER *p)
{
for (; p; p = p->next)
if (strcasecmp (s, p->attribute) == 0)
return (p->value);
return NULL;
}
/* returns 1 if Mutt can't display this type of data, 0 otherwise */
int mutt_needs_mailcap (BODY *m)
{
switch (m->type)
{
case TYPETEXT:
if (!strcasecmp ("plain", m->subtype) ||
!strcasecmp ("rfc822-headers", m->subtype) ||
!strcasecmp ("enriched", m->subtype))
return 0;
break;
#ifdef _PGPPATH
case TYPEAPPLICATION:
if (!strcasecmp ("pgp", m->subtype) ||
!strcasecmp ("pgp-signed", m->subtype) ||
!strcasecmp ("x-pgp-message", m->subtype))
return 0;
break;
#endif /* _PGPPATH */
case TYPEMULTIPART:
case TYPEMESSAGE:
return 0;
}
return 1;
}
int mutt_is_text_type (int t, char *s)
{
if (t == TYPETEXT)
return 1;
if (t == TYPEMESSAGE)
{
if (!strcasecmp ("delivery-status", s))
return 1;
}
#ifdef _PGPPATH
if (t == TYPEAPPLICATION)
{
if (!strcasecmp ("pgp-keys", s))
return 1;